package com.sun.controller;

import com.github.pagehelper.PageHelper;
import com.sun.pojo.Student;
import com.sun.utils.DataGridView;
import com.sun.utils.ExcelUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sun.misc.Request;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author sun
 * @date 2020/11/5 - 10:00
 */
@RestController
@RequestMapping("read")
public class ReadExcelController {
    @RequestMapping("readExcel")
    public DataGridView readExcel(HttpServletRequest request) {
        String filePath = request.getServletContext().getRealPath("/upload/学生成绩表.xlsx");
      //  String filePath = "E:\\Mybatis\\Experiment2_Mybatis\\springMVC\\SpringMVC_POI\\src\\test\\java\\学生成绩表.xlsx";
        ExcelUtil excelUtil = new ExcelUtil();
        List<Student> students=new ArrayList<Student>();
        try {
            List<Object>list=excelUtil.ExcelUtil(filePath);
            for (int i=0;i<list.size();i++){
                Student student=new Student();
                for(int j=0;j<=7;j++){
                    switch (j){
                        case 0:student.setId(list.remove(0).toString());break;
                        case 1:student.setName(list.remove(0).toString());break;
                        case 2:student.setChineseScore(Double.parseDouble(list.remove(0).toString()));break;
                        case 3:student.setMathScore(Double.parseDouble(list.remove(0).toString()));break;
                        case 4:student.setEnglishScore(Double.parseDouble(list.remove(0).toString()));break;
                        case 5:student.setPhysicalScore(Double.parseDouble(list.remove(0).toString()));break;
                        case 6:student.setChemicalScore(Double.parseDouble(list.remove(0).toString()));break;
                        case 7:student.setGeographyScore(Double.parseDouble(list.remove(0).toString()));break;
                    }
                }
                students.add(student);
            }
            for(Student s:students){
                System.out.println(s.toString());
                System.out.println();

            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return new DataGridView(students);
    }

}
